package com.mifengs.mall.ui.main;

import android.text.TextUtils;

import com.mifengs.commonbase.util.LogUtil;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.Constant;
import com.mifengs.mall.MallApplication;
import com.mifengs.mall.entity.FloorBean;
import com.mifengs.mall.entity.FloorResponseBean;
import com.mifengs.mall.entity.ResponseBean;
import com.mifengs.mall.netWork.NetWork;

import java.util.ArrayList;
import java.util.HashMap;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by lihaoqi on 2018/4/18.
 */

public class FloorFrgPresenter implements FloorFrgContract.Presenter{


    private FloorFrgContract.View mView;

    public FloorFrgPresenter(FloorFrgContract.View mView) {
        this.mView = mView;
    }

    public static final String TAG = "FloorFrgPresenter";

    public static final int LIKE_PAGE_LIKE = 8;


    @Override
    public void getData(final int pageNo,boolean isrefresh) {
        //pageNo 为1 ，且不是刷新，且不是第一次创建页面 使用 缓存数据
        if(pageNo==1&&!isrefresh&&(!mView.isFirst())){
            try {
                ArrayList<FloorBean> floorBeans = MallApplication.getInstance().getCacheHelper().getAsSerializable(TAG+mView.getTabId());
                if(floorBeans!=null){
                    mView.setFloors(floorBeans,false);
                    return;
                }
            }catch (Exception e){

            }
        }
        HashMap<String,String> paramsMap = new HashMap<>();
        paramsMap.put("tabId",mView.getTabId());
        paramsMap.put("pageNo",pageNo+"");
        NetWork.getHomeDataApi().getFloors(paramsMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean<FloorResponseBean>>bindToLife())
                .subscribe(new Observer<ResponseBean<FloorResponseBean>>() {
                    @Override
                    public void onCompleted() {}

                    @Override
                    public void onError(Throwable e) {
                        mView.finishRefresh();
                        mView.loadMoreFail();
                    }

                    @Override
                    public void onNext(ResponseBean<FloorResponseBean> responseBean) {
                        mView.finishRefresh();
                        if(Constant.SUCCESS_CODE.equals(responseBean.getStatus())){
                            ArrayList<FloorBean> floorBeans = (ArrayList<FloorBean>) responseBean.getParamer().getFloorsDtoList();
                            if(floorBeans.size()==6&(mView.getTabId().equals("1"))){
                                LogUtil.i(TAG,":数据加载错误");
                            }
                            if(pageNo==1){
                               if(responseBean.getParamer().getFloorSize()==floorBeans.size()) {
                                   mView.setFloors(floorBeans,true);
                               }else{
                                   mView.setFloors(floorBeans,false);
                               }
                               MallApplication.getInstance().getCacheHelper().put(TAG+mView.getTabId(),floorBeans);
                            }else if (pageNo>1){
                                int total = responseBean.getParamer().getFloorSize();
                                boolean isLoadOver = total<=(mView.getItemCount()+floorBeans.size())?true:false;
                                mView.setMoreFloors(floorBeans,isLoadOver);
                            }
                        }else{
//                            mView.showLongToast(responseBean.getInfo());
                            mView.loadMoreFail();
                        }
                    }
                });
    }



    @Override
    public void getLikeGoods(final int pageNo) {
        HashMap<String,String> paramsMap = new HashMap<>();
        paramsMap.put("orders","buys,desc");
        paramsMap.put("curPageNO",pageNo+"");
        paramsMap.put("pageSize",LIKE_PAGE_LIKE+"");
        String prodId = PreferenceUtils.getProDuctId(mView.getContext());
        if(!TextUtils.isEmpty(prodId)){
            paramsMap.put("prodId",prodId);
        }
        NetWork.getHomeDataApi().getLikeGoods(paramsMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean<FloorResponseBean>>bindToLife())
                .subscribe(new Observer<ResponseBean<FloorResponseBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable throwable) {
                        mView.loadMoreFail();
                        //mView.showLongToast(throwable.toString());
                    }

                    @Override
                    public void onNext(ResponseBean<FloorResponseBean> responseBean) {
                        if(responseBean.getStatus().equals(Constant.SUCCESS_CODE)){
                            mView.setMoreLikeFloors(responseBean.getParamer().getFloorsDtoList());
                        }else{
                            mView.loadMoreFail();
                        }
                    }
                });
    }




}
